package com.ruoyi.xqjh.lhxxqjh.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.xqjh.lhxxqjh.domain.Lhxxqjh;
import com.ruoyi.xqjh.lhxxqjh.domain.LhxxqjhMx;
import com.ruoyi.xqjh.lhxxqjh.service.ILhxxqjhService;
import com.ruoyi.xqjh.lhxxqjh.mapper.LhxxqjhMapper;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.*;

/**
 * 6号线需求计划Service业务层处理
 *
 * @author huacai
 * @date 2025-04-16
 */
@Service
public class LhxxqjhServiceImpl implements ILhxxqjhService {
    @Autowired
    private LhxxqjhMapper lhxxqjhMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询6号线需求计划
     *
     * @param lhxxqjhId 6号线需求计划主键
     * @return 6号线需求计划
     */
    @Override
    public Lhxxqjh selectLhxxqjhByLhxxqjhId(String lhxxqjhId) {
        return lhxxqjhMapper.selectLhxxqjhByLhxxqjhId(lhxxqjhId);
    }

    /**
     * 查询6号线需求计划列表
     *
     * @param lhxxqjh 6号线需求计划
     * @return 6号线需求计划
     */
    @Override
    public List<Lhxxqjh> selectLhxxqjhList(Lhxxqjh lhxxqjh) {
        return lhxxqjhMapper.selectLhxxqjhList(lhxxqjh);
    }

    /**
     * 新增6号线需求计划
     *
     * @param lhxxqjh 6号线需求计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLhxxqjh(Lhxxqjh lhxxqjh) {
        lhxxqjh.setLhxxqjhId(IdUtils.fastSimpleUUID());
        lhxxqjh.setCreateTime(DateUtils.getNowDate());
        lhxxqjh.setCreateBy(getUsername());
        lhxxqjh.setUserId(Math.toIntExact(getUserId()));
        lhxxqjh.setDeptId(Math.toIntExact(getDeptId()));
        int rows = lhxxqjhMapper.insertLhxxqjh(lhxxqjh);
        insertLhxxqjhMx(lhxxqjh);
        return rows;
    }

    /**
     * 批量新增6号线需求计划
     *
     * @param lhxxqjhs 6号线需求计划List
     * @return 结果
     */
    @Override
    public int batchInsertLhxxqjh(List<Lhxxqjh> lhxxqjhs) {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(lhxxqjhs)) {
            try {
                for (int i = 0; i < lhxxqjhs.size(); i++) {
                    int row = lhxxqjhMapper.insertLhxxqjh(lhxxqjhs.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i > 0 && i % 100 == 0) || i == lhxxqjhs.size() - 1;
                    if (bool) {
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            } catch (Exception e) {
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            } finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改6号线需求计划
     *
     * @param lhxxqjh 6号线需求计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLhxxqjh(Lhxxqjh lhxxqjh) {
        lhxxqjh.setUpdateTime(DateUtils.getNowDate());
        lhxxqjh.setCreateBy(getUsername());
        lhxxqjh.setUserId(Math.toIntExact(getUserId()));
        lhxxqjh.setDeptId(Math.toIntExact(getDeptId()));
        lhxxqjhMapper.deleteLhxxqjhMxByLhxxqjhId(lhxxqjh.getLhxxqjhId());
        insertLhxxqjhMx(lhxxqjh);
        return lhxxqjhMapper.updateLhxxqjh(lhxxqjh);
    }

    /**
     * 批量删除6号线需求计划
     *
     * @param lhxxqjhIds 需要删除的6号线需求计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLhxxqjhByLhxxqjhIds(String[] lhxxqjhIds) {
        lhxxqjhMapper.deleteLhxxqjhMxByLhxxqjhIds(lhxxqjhIds);
        return lhxxqjhMapper.deleteLhxxqjhByLhxxqjhIds(lhxxqjhIds);
    }

    /**
     * 删除6号线需求计划信息
     *
     * @param lhxxqjhId 6号线需求计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLhxxqjhByLhxxqjhId(String lhxxqjhId) {
        lhxxqjhMapper.deleteLhxxqjhMxByLhxxqjhId(lhxxqjhId);
        return lhxxqjhMapper.deleteLhxxqjhByLhxxqjhId(lhxxqjhId);
    }

    /**
     * 新增6号线需求计划明细信息
     *
     * @param lhxxqjh 6号线需求计划对象
     */
    public void insertLhxxqjhMx(Lhxxqjh lhxxqjh) {
        List<LhxxqjhMx> lhxxqjhMxList = lhxxqjh.getLhxxqjhMxList();
        String lhxxqjhId = lhxxqjh.getLhxxqjhId();
        if (StringUtils.isNotNull(lhxxqjhMxList)) {
            List<LhxxqjhMx> list = new ArrayList<LhxxqjhMx>();
            for (LhxxqjhMx lhxxqjhMx : lhxxqjhMxList) {
                lhxxqjhMx.setLhxxqjhId(lhxxqjhId);
                list.add(lhxxqjhMx);
            }
            if (list.size() > 0) {
                lhxxqjhMapper.batchLhxxqjhMx(list);
            }
        }
    }
}
